package net.roobook.cms.modules.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.roobook.cms.common.entity.SearchEntity;
import net.roobook.cms.common.entity.StateCodeEnum;
import net.roobook.cms.common.exception.CmsException;
import net.roobook.cms.common.service.CrudService;
import net.roobook.cms.modules.admin.dao.CategoryMapper;
import net.roobook.cms.modules.admin.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author cos
 * @email 327878610@qq.com
 * @link https://roobook.net
 * @Description TODO
 * @date 2023/9/26 16:49
 **/
@Service
public class CategoryService extends CrudService<CategoryMapper, Category> {
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 查看子栏目
     */
    public List<Category> selectByParentId(String id) {
        List<Category> list = categoryMapper.selectByParentId(id);
        if (CollUtil.isNotEmpty(list)){
            list.forEach(i->{
                List<Category> childrenNodes = categoryMapper.selectByParentId(i.getId());
                i.setChildrenFlag(CollUtil.isEmpty(childrenNodes)?0:1);
            });
        }
        return list;
    }


    /**
     *  列表
     */
    @Override
    public PageInfo<Category> queryListByPage(SearchEntity params) {
        if(params.getPageNum() == null || params.getPageNum() == 0) {
            params.setPageNum(1);
        }
        if(params.getPageSize() == null || params.getPageSize() == 0) {
            params.setPageSize(10);
        }
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Category> list = categoryMapper.queryListByPage(params.getEntity());
        if (CollUtil.isNotEmpty(list)){
            list.forEach(i->{
                List<Category> childrenNodes = categoryMapper.selectByParentId(i.getId());
                i.setChildrenFlag(CollectionUtils.isEmpty(childrenNodes)?0:1);
            });
        }
        PageInfo<Category> page = new PageInfo<Category>(list);
        return page;
    }


    /**
     * 删除
     */
    @Override
    @Transactional
    public int delete(String id){
        Category category = categoryMapper.selectByPrimaryKey(id);
        return cascadeDelete(category);
    }





    /**
     * 级联删除栏目
     * @param category
     * @return
     */
    private int cascadeDelete(Category category) {
        int counter = 0;
        if(category == null) {
            return counter;
        }
        List<Category> categorys = categoryMapper.selectByParentId(category.getId());
        for(Category temp : categorys) {
            counter += cascadeDelete(temp);
        }
        counter += categoryMapper.deleteByPrimaryKey(category.getId());
        return counter;
    }
}
